﻿var bingKmlParser = function() {
    // private variables
    var _styles = {};
    var _styleMap = {};

    // private methods

    function _parsePolygons(placemarkXml) {
        var vertices;
        $(placemarkXml).find('Polygon').find('coordinates').each(function(index, coordinates) {
            vertices = _parseVertices(coordinates.firstChild.data);
        });

        var colorName = $(placemarkXml).find('styleUrl').contents()[0].data.substr(1);;
        var polygonOptions = _styleMap[colorName];

        var polygon = new Microsoft.Maps.Polygon(vertices, polygonOptions);

        return polygon;
    }

    function _parseVertices(coordinateString) {
        coordinateString = $.trim(coordinateString);
        var coordinates = coordinateString.split(' ');
        var msLocations = [];

        for (i=0; i<coordinates.length; i++) {
            var coordinate = coordinates[i];
            var xyz = coordinate.split(',');

            var location = new Microsoft.Maps.Location(xyz[1],xyz[0],xyz[2]);
            msLocations.push(location);
        }

        return msLocations;
    };

    /**
	 * Parse a KML color string that comes as a abgr hex string.
	 * @param kmlColorString An abgr hex string
	 * @return {Microsoft.Maps.Color}
	 * @private
	 */
    function _parseColor(kmlColorString) {
        // Our KML files have color strings as hex in abgr order. Here we swap them to argb order.
        var hexArray = [kmlColorString.charAt(0) + kmlColorString.charAt(1),
			kmlColorString.charAt(6) + kmlColorString.charAt(7),
			kmlColorString.charAt(4) + kmlColorString.charAt(5),
			kmlColorString.charAt(2) + kmlColorString.charAt(3)];

        var argbArray = [];
        for (i=0; i<hexArray.length; i++) {
            argbArray.push(parseInt(hexArray[i], 16));
        }

        return new Microsoft.Maps.Color(argbArray[0], argbArray[1], argbArray[2], argbArray[3]);
    };

    /**
	 * Create a map of style names to styles (PolygonOptions - http://msdn.microsoft.com/en-us/library/gg427596.aspx).
	 * @param kmlXml
	 * @return {Object} a map of <String, PolygonOptions>
	 * @private
	 */
    function _parseStyles(kmlXml) {
        $(kmlXml).find('Style').each(function(index, styleXml) {
            var styleId = $(styleXml).attr('id');
            var msPolygonOptions = {};

            if ($(styleXml).find('PolyStyle').length > 0) {
                msPolygonOptions.fillColor = _parseColor($(styleXml).find('PolyStyle').find('color').contents()[0].data);
                msPolygonOptions.strokeColor = _parseColor($(styleXml).find('LineStyle').find('color').contents()[0].data);
                msPolygonOptions.strokeThickness = parseInt($(styleXml).find('LineStyle').find('width').contents()[0].data);
            }

            _styles[styleId] = msPolygonOptions;
        });

        $(kmlXml).find('StyleMap').each(function(index, styleMapXml) {
            var styleMapId = $(styleMapXml).attr('id');
            var mappedStyle = $(styleMapXml).find('styleUrl').contents()[0].data.substr(1);

            _styleMap[styleMapId] = _styles[mappedStyle];
        });
    }

    /**
	 * public functions that are available using this object.
	 */
    var _public = {
        parsePolygons: function(kmlXml) {
            var polygons = [];

            _parseStyles(kmlXml);

            $(kmlXml).find('Placemark').each(function(index, placemarkXml) {
                polygons.push(_parsePolygons(placemarkXml));
            });

            return polygons;
        }
    };

    return
} 